import com.mysql.cj.jdbc.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.*;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.Executor;

// jdbc查询
public class demo2 {
    public static void main(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java109?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("502502");

        Connection connection = dataSource.getConnection();

        // 构造sql
        String sql = "select * from student";
        PreparedStatement statement = connection.prepareStatement(sql);

        // 执行sql ResultSet就是查询结果的集合（临时表）
        ResultSet resultSet = statement.executeQuery();

        while(resultSet.next())
        {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            System.out.println("id = " + id + ", name = " + name);
        }

        resultSet.close();
        statement.close();
        connection.close();
    }
}
